<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<head>
<script src="/js/jquery-3.5.1.min.js"></script>
</head>

<div align="center" style="margin-top: 100px;">
    <h2>登录</h2>
    <p>
                <form action="/login" method="post">
                     <table align="center">
                            <tr>
                                <td >登陆名</td>
                                <td ><input id="name" name="name" placeholder="输入您的登陆名" > </td>
                            </tr>
                            <tr>
                                <td >登陆密码</td>
                                <td ><input id="password" name="password" type="password"  placeholder="输入您的登陆密码" > </td>
                            </tr>
                            <tr>
                                <td >
                                    <button id="loginBtn" type="submit">登   录</button>
                                </td>

                            </tr>
                      </table>
                </form>
    </p>
</div>

<script type="text/javascript">

    $(document).ready(function(){
        $("#loginBtn").click(function(){

           var msg = "";
           var nameValue = $("#name").val();
           if(!nameValue){
               msg += "用户名不能为空！"+"\n";
           }

           var psw = $("#password").val();
           if(!psw){
               msg += "密码不能为空！" + "\n";
           }

           if(msg != ""){
               alert(msg);
               return false;
           }

            res = "";
            $.ajax({
                type :"post",
                url :"/userExists",
                data :{ name : nameValue },
                async :false,
                success :function(data){
                    res = data;
                }});
             if(res == "false"){
                 alert("用户 '" + nameValue + "' 不存在！");
                 return false;
             }

             isUser = "111";
             $.ajax({
                type:"post",
                url:"/isUser",
                data:{
                    name : nameValue,
                    password : psw
                },
                async: false,
                success : function(data){
                    isUser = data;
                }
             });

             if(isUser == "false"){
                 alert("密码错误！");
                 return false;
             }

        });

    });

</script>